package com.wanmait.movie.webController;

import com.wanmait.movie.entity.User;
import com.wanmait.movie.entity.VO.UserDTO;
import com.wanmait.movie.service.UserService;
import com.wanmait.movie.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("web/user")
public class WebUserController {
    @Resource
    private UserService userService;

    @GetMapping("getUserInformation")
    public AjaxResult getUserInformation(HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        return AjaxResult.success(user);
    }

    @PostMapping("information")
    public AjaxResult information(@RequestBody UserDTO userDTO, HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        user.setAvatar(userDTO.getAvatar());
        user.setBirthday(userDTO.getBirthday());
        user.setGender(userDTO.getGender());
        user.setHobby(userDTO.getHobby());
        user.setUsername(userDTO.getUsername());
        userService.saveOrUpdate(user);
        return AjaxResult.success();
    }

}
